package com.tfswx.jcyw.ruleenginedesigner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtMxstgxMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtMxstgx;
import com.tfswx.jcyw.ruleenginedesigner.dto.mxstgxgl.*;
import com.tfswx.jcyw.ruleenginedesigner.service.MxstgxService;
import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.compute.util.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/7/3
 */
@Service
public class MxstgxServiceImpl implements MxstgxService {

    @Resource
    private GzyqXtMxstgxMapper gzyqXtMxstgxMapper;

    @Override
    public List<MxstgxListByXmbmQueryOutputDTO> queryMxstgxListByXmbm(MxstgxListByXmbmQueryInputDTO mxstgxListByXmbmQueryInputDTO) {
        GzyqXtMxstgx gzyqXtMxstgx = new GzyqXtMxstgx();
        BeanUtils.copyProperties(mxstgxListByXmbmQueryInputDTO, gzyqXtMxstgx);
        return gzyqXtMxstgxMapper.queryMxstgxListByXmbm(gzyqXtMxstgx);
    }

    @Override
    public String addMxstgx(MxstgxAddInputDTO mxstgxAddInputDTO) {
        GzyqXtMxstgx gzyqXtMxstgx = new GzyqXtMxstgx();
        BeanUtils.copyProperties(mxstgxAddInputDTO, gzyqXtMxstgx);
        gzyqXtMxstgx.setMxstgxbm(CommonUtil.getUuid());
        gzyqXtMxstgx.setSfsc("N");
        gzyqXtMxstgx.setCjsj(new Date());
        gzyqXtMxstgx.setZhxgsj(new Date());
        int i = gzyqXtMxstgxMapper.insert(gzyqXtMxstgx);
        if (i != 1) {
            throw new AppException("添加失败");
        }
        return gzyqXtMxstgx.getMxstgxbm();
    }

    @Override
    public boolean deleteMxstgx(MxstgxDeleteInputDTO mxstgxDeleteInputDTO) {
        GzyqXtMxstgx gzyqXtMxstgx = new GzyqXtMxstgx();
        BeanUtils.copyProperties(mxstgxDeleteInputDTO, gzyqXtMxstgx);
        gzyqXtMxstgx.setZhxgsj(new Date());
        int i = gzyqXtMxstgxMapper.delete(gzyqXtMxstgx);
        if (i != 1) {
            throw new AppException("删除失败");
        }
        return true;
    }

    @Override
    public boolean updateMxstgx(MxstgxUpdateInputDTO mxstgxUpdateInputDTO) {
        GzyqXtMxstgx gzyqXtMxstgx = new GzyqXtMxstgx();
        BeanUtils.copyProperties(mxstgxUpdateInputDTO, gzyqXtMxstgx);
        gzyqXtMxstgx.setZhxgsj(new Date());
        int i = gzyqXtMxstgxMapper.update(gzyqXtMxstgx);
        if (i != 1) {
            throw new AppException("修改失败");
        }
        return true;
    }

    @Override
    public int qkMxstgx(MxstgxQkInputDTO mxstgxQkInputDTO) {
        List<String> xmbmList = mxstgxQkInputDTO.getXmbmList();
        if (xmbmList == null || xmbmList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < xmbmList.size(); i += 500) {
            int end = i + 500;
            if (end > xmbmList.size()) {
                end = xmbmList.size();
            }
            List<String> strings = xmbmList.subList(i, end);
            count += gzyqXtMxstgxMapper.qkMxstgx(strings);
        }
        return count;
    }
}
